﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Globalization;
using AutoMapper;
using System.Text;


namespace WaiGaoQiaoWeb.Controllers
{
    public class TbtSporadicAppController : Controller
    {
        //
        // GET: /Consumables/

        public ActionResult Index()
        {
            return View();
        }
        protected TbtSporadicApp instockSp = new TbtSporadicApp(true);
        private Con_GoodsInfo conInfo = new Con_GoodsInfo(true);
        /// 读取食材入库明细
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetDetailList(PageVM pvm)
        {
            if (string.IsNullOrEmpty(pvm.query))
            {
                pvm.query = "and OrderNo = ''";
            }
            string condition = pvm.query;//"and OrderNo = '" + pvm.query + "'";
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = instockSp.GetRowCount(condition);

            IList<TbtSporadicApp> splist = instockSp.GetInstockGroup(pvm.rp, pvm.page, condition);

            List<string> v;
            foreach (TbtSporadicApp ida in splist)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.AppDt.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo));
                v.Add(Con_GoodsInfo.GetGoodsNameByID(ida.Goods_ID));
                v.Add(ida.SpType == "1" ? "保洁用品" : (ida.SpType == "2" ? "餐橱用品" : "材料及文办用品"));
                v.Add(ida.Count.ToString());
                v.Add(ida.UserMan);
                v.Add(ida.Remark);
                v.Add(ida.RecordDateTime.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo));
                v.Add(ida.OperatorID.ToString());
                v.Add(ida.OrderNo);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);

        }

        /// 读取食材入库明细
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetDetailListByOrderNo(PageVM pvm)
        {
            string condition = "and OrderNo = '" + pvm.query + "'";
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = instockSp.GetRowCount(condition);

            IList<TbtSporadicApp> splist = instockSp.GetInstockGroup(pvm.rp, pvm.page, condition);

            List<string> v;
            foreach (TbtSporadicApp ida in splist)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.AppDt.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo));
                v.Add(Con_GoodsInfo.GetGoodsNameByID(ida.Goods_ID));
                v.Add(ida.SpType == "1" ? "保洁用品" : (ida.SpType == "2" ? "餐橱用品" : "材料及文办用品"));
                v.Add(ida.Count.ToString());
                v.Add(ida.UserMan);
                v.Add(ida.Remark);
                v.Add(ida.RecordDateTime.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo));
                v.Add(ida.OperatorID.ToString());
                v.Add(ida.OrderNo);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);

        }


        //*******************************************************
        //  新建食材明细
        //*******************************************************
        public ActionResult Add(int? id)
        {
            ConsumablesDetailVM idvm = new ConsumablesDetailVM();
            if (id == null)
            {
                idvm.ID = 0;
                idvm.Count = 0;
                idvm.RecordDateTime = DateTime.Now.ToString("yyyy-MM-dd");
                idvm.OperatorID = ((LogOnModel)System.Web.HttpContext.Current.Session["user"]).LogName;
                idvm.OrderNo = instockSp.GetInstockSporadicOrderNo();
                idvm.InstockDt1 = DateTime.Now.ToString("yyyy-MM-dd");
            }
            else
            {

                TbtSporadicApp idinfo = instockSp.GetInstockGroup(Convert.ToInt32(id));
                Mapper.DynamicMap(idinfo, idvm);
                idvm.InstockDt1 = idinfo.AppDt.ToString("yyyy-MM-dd");
                idvm.RecordDateTime = idinfo.RecordDateTime.ToString("yyyy-MM-dd");
            }
            ViewData["SpType"] = GetSpType();
            return View(idvm);
        }


        [HttpPost]
        public JsonResult Save(ConsumablesDetailVM cdvm)
        {
            TbtSporadicApp info = new TbtSporadicApp();
            info.ID = cdvm.ID;
            info.Count = cdvm.Count;
            info.AppDt = Convert.ToDateTime(cdvm.InstockDt1);
            info.Goods_ID = cdvm.Goods_ID;
            info.SpType = cdvm.SpType;
            info.UserMan = cdvm.UserMan;
            info.Remark = cdvm.Remark;
            info.RecordDateTime = Convert.ToDateTime(cdvm.RecordDateTime);
            info.OperatorID = cdvm.OperatorID;
            info.OrderNo = cdvm.OrderNo;
            cdvm.Goods_ID1 = cdvm.Goods_ID.ToString();
            if (ModelState.IsValid)
            {
                instockSp.SaveSundries(info);
            }
            return Json(true);
        }
        [HttpPost]
        public JsonResult Delete(string id)
        {
            instockSp.DeleteInstockGroup(id);
            return Json(true);
        }
        /// <summary>
        /// 获取商品类型
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetSpType()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "保洁用品");
            dic.Add(2, "餐橱用品");
            dic.Add(3, "材料及文办用品");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }
        /// <summary>
        /// 输出到Excel
        /// </summary>
        /// <param name="yy"></param>
        /// <param name="mm"></param>
        /// <returns></returns>
        public ActionResult Exprot(string time, string name)
        {
            return EContent(time, name);
        }
        public ExcelContentResult EContent(string time, String name)
        {
            StringBuilder sBuilder = new StringBuilder();
            if (!string.IsNullOrEmpty(time))
            {
                sBuilder.AppendFormat(" AND AppDt='{0}'", time);
            }
            if (!string.IsNullOrEmpty(name))
            {
                sBuilder.AppendFormat(" AND Goods_ID='{0}'", name);
            }
            IList<TbtSporadicApp> splist = instockSp.GetInstockGroup(instockSp.GetRowCount(sBuilder.ToString()), 1, sBuilder.ToString());
            sBuilder.Clear();
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='0'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='5' style='font-size:25px; text-align:center;'>物品申请单</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td style='text-align:right;width:50px;'>日期</td>");
            sBuilder.Append("<td style='text-align:left;width:200px;'>" + time + "</td>");
            sBuilder.Append("<td style='width:100px;'></td>");
            sBuilder.Append("<td style='text-align:right;width:100px;'>物品名称</td>");
            sBuilder.Append("<td style='text-align:left;width:100px;'>" + name + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");

            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td style='width:50px;'>序号</td>");
            sBuilder.Append("<td style='width:200px;'>品名</td>");
            sBuilder.Append("<td style='width:100px;'>数量</td>");
            sBuilder.Append("<td style='width:100px;'>使用人</td>");
            sBuilder.Append("<td style='width:100px;'>备注</td>");
            sBuilder.Append("</tr>"); //style='vnd.ms-excel.numberformat:@'
            decimal sumamount = 0.00M;
            foreach (TbtSporadicApp item in splist)
            {
                sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                sBuilder.Append("<td style='width:50px;'>" + item.ID + "</td>");
                sBuilder.Append("<td style='width:200px;'>" + Con_GoodsInfo.GetGoodsNameByID(Convert.ToInt32(item.Goods_ID)) + "</td>");
                sBuilder.Append("<td style='width:100px;'>" + item.Count.ToString() + "</td>");
                sBuilder.Append("<td style='width:100px;'>" + item.UserMan.ToString() + "</td>");
                sBuilder.Append("<td style='width:100px;'>" + item.Remark + "</td>");
                sBuilder.Append("</tr>"); //style='vnd.ms-excel.numberformat:@'
                sumamount = sumamount + Convert.ToDecimal(item.Count);
            }
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td>总数量</td>");
            sBuilder.Append("<td>" + sumamount.ToString() + "</td>");
            sBuilder.Append("</tr>"); //style='vnd.ms-excel.numberformat:@'
            sBuilder.Append("</table>");


            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "TbtSporadicApp";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }

    }
}
